privatestaticfinal SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
//每5秒执行 @Scheduled(fixedRate = 5000) publicvoidreportCurrentTime(){ log.info("The time is now {}", dateFormat.format(new Date())); } }
运行程序,查看输出:
1 2 3 4 5 6 7 8
... 2018-04-09 14:43:08.631 INFO 13273 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-04-09 14:43:08.636 INFO 13273 --- [ main] com.tt.study.demo.DemoApplication : Started DemoApplication in 7.855 seconds (JVM running for 8.709) 2018-04-09 14:43:13.608 INFO 13273 --- [pool-5-thread-1] c.t.s.demo.service.schedule.TestTask : The time is now 14:43:13 2018-04-09 14:43:18.609 INFO 13273 --- [pool-5-thread-1] c.t.s.demo.service.schedule.TestTask : The time is now 14:43:18 2018-04-09 14:43:23.609 INFO 13273 --- [pool-5-thread-1] c.t.s.demo.service.schedule.TestTask : The time is now 14:43:23 2018-04-09 14:43:28.608 INFO 13273 --- [pool-5-thread-1] c.t.s.demo.service.schedule.TestTask : The time is now 14:43:28 2018-04-09 14:43:33.608 INFO 13273 --- [pool-5-thread-1] c.t.s.demo.service.schedule.TestTask : The time is now 14:43:33